package library;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JTextPane;





public class LoginPane extends JPanel {

	private String userName;
	private String userPassword;
	private TextPane infoEntry;
	private String name = "Rozłączony";
	//private JPasswordField passwordField = new JPasswordField();
	//private TextPane nameField;
	
	private LibraryMainFrame parent = null;
	JButton showLogin = new JButton("Połącz");
	LoginInfoEntryPane pane = new LoginInfoEntryPane();
	
	public LoginPane(LibraryMainFrame mainFrame) {
		//nameField = new TextPane(new String[1] = new String("user id: "), new JTextField());
		this.parent = mainFrame;
		this.add(pane);
		showLogin.addActionListener(new ShowLoginActionListener());
		this.add(showLogin);
		this.setMaximumSize(new Dimension(200, 200));
		showLogin.setVisible(true);
		pane.setVisible(false);
		
		this.setVisible(true);
	}
	
	private class ShowLoginActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			
			showLogin.setVisible(false);
			pane.setVisible(true);
			
		}
		
	}
	
	
	private class LoginInfoEntryPane extends JPanel {

		public LoginInfoEntryPane() {
			this.setLayout(new BorderLayout());
			String[] names = new String[2];
			names[0] = "id: ";
			names[1] = "password";
			infoEntry = new TextPane(names, new JTextField(), new JPasswordField());
			//this.setLayout(new GridLayout(4, 1));
			this.add(infoEntry, BorderLayout.CENTER);
			JPanel decisionPanel = new JPanel(new GridLayout(1, 2));
			JButton logIn = new JButton("połącz");
			JButton abort = new JButton("anuluj");
			abort.addActionListener(new AbortLoginActionListener());
			logIn.addActionListener(new AgreeListener());
			decisionPanel.add(logIn);
			decisionPanel.add(abort);
			this.add(decisionPanel, BorderLayout.SOUTH);
		}
		
		
		private class AbortLoginActionListener implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				showLogin.setVisible(true);
				pane.setVisible(false);
				
			}
			
		}
		
		private class AgreeListener implements ActionListener {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("LoginPane:  Click!!!");
				JPasswordField pass = (JPasswordField) infoEntry.getField("password");
				userPassword = String.valueOf(pass.getPassword());
				userName = infoEntry.getFieldText("id: ");
				
				userName = "librarian";
				userPassword = "librarian";
				
				try {
					Class.forName("oracle.jdbc.driver.OracleDriver");
					try {
						
						Connection con = DriverManager.getConnection(
								"jdbc:oracle:thin:@//linux-w827.site:1521/XE",
								userName, userPassword);
						
						con.setAutoCommit(false);
						System.out.println("LoginPane:  sets Connection");
						parent.setConnection(con, userName, userPassword);
						
					} catch (SQLException ex) {
						System.out.println("Connection fail");
						ex.printStackTrace();
					}
				} catch (ClassNotFoundException ex) {
					// TODO Auto-generated catch block
					System.out.println("error");
					ex.printStackTrace();
				} 
				
			}
		}
	}
}
